package com.itheima.jobs;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;
@Component
public class ClearImages {

    @Autowired
    private JedisPool jedisPool;

    public void run(){
        /*先比较两个集合SETMEAL_PIC_RESOURCES  SETMEAL_PIC_DB_RESOURCES*/
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        if (CollectionUtil.isNotEmpty(sdiff)){
            //删除七牛中的图片
            for (String img : sdiff) {
                QiniuUtils.deleteFileFromQiniu(img);
                //删除Redis中的图片。以防下次查询出来
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,img);
            }
        }

    }


}
